package jschool.net.servidor;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.net.Socket;

import jschool.net.Comando;

public class Conexion implements Runnable {
	
	private Socket socket;
	
	private InputStream input = null;
	private OutputStream output = null;
	private DataInputStream in = null;
	private DataOutputStream out = null;
	private ObjectOutputStream oos = null;
	private ObjectInputStream ois = null;
	
	public Conexion( Socket socket ) {
		
		this.socket = socket;
		crearComunicacion();
	}
	
	
	public void run() {
		
		Comando comando;
		
		while( true ) {
			
			try {
				comando = leerComando( ois.readObject() );
				
				switch( comando ) {
				
				case LOGIN:
					
					break;
				
				case AGREGAR_ALUMNO:
					
					break;
					
				case BORRAR_ALUMNO:
					
					break;
				}
				
			}
			catch( IOException ioe ) {
				ioe.printStackTrace();
			}
			catch( ClassNotFoundException cnfe ) {
				cnfe.printStackTrace();
			}
		}
	}
	
	
	public Comando leerComando( Object o ) {
		return ( Comando ) o;
	}
	
	public void crearComunicacion()
	{		
		try {
			
			output = socket.getOutputStream();
			out = new DataOutputStream( output );
			oos = new ObjectOutputStream ( output );
		}

		catch( IOException ioe ) {
			
			ioe.printStackTrace();
			System.out.println("Error al crear DataOutputStream");
		}	
		
//---------------------------
		try {
		
			input = socket.getInputStream();
			in = new DataInputStream ( input );
			ois = new ObjectInputStream( input );
		}
			
		catch( IOException ioe ) {
		
			ioe.printStackTrace();
			System.out.println("Error al crear DataInputStream");
		}			
		
	}
	
//--------------------------------------------------------------------------------	
	
	
	public void cerrarComunicacion() {
		try {
		
			in.close();
			input.close();

			out.close();
			output.close();
		}
		catch ( IOException ioe ) {
			
			ioe.printStackTrace();		
			System.out.println("\n\nERROR AL CERRAR LA CONEXION" );
		}
	}	
//--------------------------------------------------------------------------------
}
